package com.aisile.service.impl;

import com.aisile.admin.AdChannelApi;
import com.aisile.mapper.AdChannelMapper;
import com.aisile.model.admin.dtos.ChannelDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.service.AdChannelService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServieImpl  extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    //全查频道
    @Override
    public ResponseResult findAll(ChannelDto channelDto) {
//        首先判断dto是否为空
        if(channelDto==null){
            return new ResponseResult(201,"空值",channelDto);
        }
        //配置分页 为什么可以调是因为前端传的json转json的时候就new对象了
        channelDto.checkParam();
        //2.判断条件就是name模糊查询，不为空
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(channelDto.getName()),AdChannel::getName,channelDto.getName());
        //3.page分页
        IPage<AdChannel> pageInfo=new Page<>(channelDto.getPage(),channelDto.getSize());
        IPage<AdChannel> page = this.page(pageInfo,lambdaQueryWrapper);
        //4.返回
        ResponseResult responseResult=new PageResponseResult(channelDto.getPage(),channelDto.getSize(),Integer.valueOf(page.getTotal()+""));
        //5.把集合返回
        responseResult.setData(page.getRecords());
        return responseResult;
    }
        //添加频道
    @Override
    public ResponseResult saveAdChennal(AdChannel adChannel) {
        //先判断频道是否为空
        if(adChannel==null){
            return new ResponseResult(20001,"为空",adChannel);
        }
          //然后在查询数据有没有重复
       LambdaQueryWrapper<AdChannel> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(adChannel.getName()!=null,AdChannel::getName,adChannel.getName());
        Integer integer = this.baseMapper.selectCount(wrapper);
        //如果大于0报错
        if(integer>0){
            return new ResponseResult(20002,"数据重复",adChannel);
        }
        //然后就是添加进去
        boolean save = this.save(adChannel);
        return new ResponseResult(200,"添加成功",save);
    }
       //修改频道
    @Override
    public ResponseResult updateAdCennal(AdChannel adChannel) {
        //首先判断为不为空
        if(adChannel==null){
            return new ResponseResult(20001,"频道不存在",adChannel);
        }
        //先查询数据在不在
       LambdaQueryWrapper<AdChannel> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(adChannel.getName()),AdChannel::getName,adChannel.getName());
        Integer integer = this.baseMapper.selectCount(wrapper);
        //在判断他在不咋在的话修改
        if(integer<0){
            return new ResponseResult(20001,"频道添加的数据不在",adChannel);
        }
        adChannel.setCreatedTime(new Date());
        boolean b = this.updateById(adChannel);

        return new ResponseResult(200,"修改成功",b);
    }

    @Override
    public ResponseResult deleteAdCennal(@PathVariable int id) {

        //删除
        boolean b = this.removeById(id);
        return new ResponseResult(200,"删除成功",b);
    }
    @Override
    public ResponseResult delManyAdCennal(@RequestBody List<Integer> list) {
        //批量删除
        boolean b = this.removeByIds(list);
        return new ResponseResult(200,"批量删除成功",b);
    }
    //查找id
    @Override
    public ResponseResult getbyId(int id) {
        AdChannel byId = this.getById(id);
        return  new ResponseResult(200,"查找成功",byId);
    }

}
